﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AvataSys : MonoBehaviour {

	public static AvataSys instance;

	//来源模型
	private Transform source;
	//目标骨架
	private Transform target;

	private Dictionary<string,Dictionary<string,SkinnedMeshRenderer>> sourceData = new Dictionary<string, Dictionary<string, SkinnedMeshRenderer>>();

//	private Dictionary<string,SkinnedMeshRenderer> targetSmr = new Dictionary<string, SkinnedMeshRenderer>();

	//new 01
	SkinnedMeshRenderer targetSmr;
	List<CombineInstance> combineInstance = new List<CombineInstance>();
	List<Material> materials = new List<Material>();
	List<Transform> bones = new List<Transform>();

	private Transform[] hips;

	string[,] avataStr = new string[,]{{"face","1"},{"hair","1"},{"pants","1"},{"shoes","1"},{"top","1"},{"eyes","1"}};
	// Use this for initialization
	void Start () {
		instance = this;
		InstantiateSource();
		InstantiateTarget();
		SaveData();
		InitAvatar();

		target.animation.PlayQueued("walk");
	}

	void Update()
	{
		if (Input.GetMouseButtonDown(0)) {
			ChangeMesh("hair","2");
		}
		if (Input.GetMouseButtonDown(1)) {
			ChangeMesh("shoes","2");
		}
		if (Input.GetMouseButtonDown(2)) {
			InitAvatar();
		}
	}

	void InstantiateSource(){
		GameObject sourceObj = Instantiate (Resources.Load("Source")) as GameObject;
		source = sourceObj.transform;
		sourceObj.SetActive(false);
	}

	void InstantiateTarget(){
		GameObject targetObj = Instantiate(Resources.Load("Target")) as GameObject;
		target = targetObj.transform;
		hips = target.GetComponentsInChildren<Transform>();
		//new 02
		targetSmr = target.gameObject.AddComponent<SkinnedMeshRenderer>();
	}

	void SaveData(){
		if ((source == null) && (target == null)) {
			return ;
		}
		SkinnedMeshRenderer[] parts = source.GetComponentsInChildren<SkinnedMeshRenderer>(true);//true表示把隐藏部位的组件也获得
		foreach (SkinnedMeshRenderer part in parts) {
			string[] partName = part.name.Split('-');
			if (!sourceData.ContainsKey(partName[0])) {
//				GameObject partObj = new GameObject();
//				partObj.name = partName[0];
//				partObj.transform.parent = target;

				sourceData.Add(partName[0], new Dictionary<string, SkinnedMeshRenderer>());

//				targetSmr.Add(partName[0], partObj.AddComponent<SkinnedMeshRenderer>());
			}
			sourceData[partName[0]].Add(partName[1],part);
		}

	}

	void ChangeMesh(string part, string num){
		SkinnedMeshRenderer smr = sourceData[part][num];

//		List<Transform> bones =new List<Transform>();

		foreach (Transform bone in smr.bones) {
			foreach (Transform hip in hips) {
				if (hip.name == bone.name) {
					bones.Add(hip);
					break;
				}
			}
		}

//		targetSmr[part].materials = smr.materials;//更换材质
//		targetSmr[part].sharedMesh = smr.sharedMesh;//更换mesh
//		targetSmr[part].bones = bones.ToArray();//将mesh绑定到合适的骨架中

		//new 03
		CombineInstance ci = new CombineInstance();
		ci.mesh = smr.sharedMesh;
		combineInstance.Add(ci);
		materials.AddRange(smr.materials);
	}

	//new 04  
	public void InitAvatar()  
	{  
		Empty();  
		int length = avataStr.GetLength(0);  
		for (int i = 0; i < length; i++)  
		{  
			ChangeMesh(avataStr[i, 0], avataStr[i, 1]);  
		}  
		Combine();  
	}  
	
	//new 05  
	void Empty()  
	{  
		combineInstance.Clear();  
		materials.Clear();  
		bones.Clear();  
	}  
	
	//new 06  
	void Combine()  
	{  
		targetSmr.sharedMesh = new Mesh();  
		targetSmr.sharedMesh.CombineMeshes(combineInstance.ToArray(),false,false);  
		targetSmr.materials = materials.ToArray();  
		targetSmr.bones = bones.ToArray();  
	}   
}
